<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/11
 * Time: 11:01
 */
namespace app\api\model;
use think\Controller;
use think\Model;
use think\Db;
use app\api\model\Hotel;
use app\api\model\Tour;
class Collect extends Model
{
    /**
     *我的收藏
     */
    public function collects($uid,$type,$page,$latitude='',$longitude='')
    {
        if($type==1){
            $res = Db::name('collect')->alias('c')->join('tours t','c.other_id=t.tour_id','LEFT')
                ->field('t.tour_id,t.tour_name,t.tour_covers,t.tour_tag,t.sale_sum')
                ->where('c.uid',$uid)
                ->where('c.type',1)
                ->limit(($page-1)*config('page_size'),config('page_size'))
                ->order('c.create_time','desc')
                ->select();
            if(!empty($res)){
                foreach ($res as $k=>$v){

                    //查询门票最低价
                    $res[$k]['price'] = Db::name('tour_ticket')
                        ->field('price')
                        ->where('tour_id',$v['tour_id'])
                        ->where('is_use',1)
                        ->where('is_del',0)
                        ->min('price');
                    //处理景点没上架门票的情况
                    if($res[$k]['price']==0){
                        $res[$k]['price'] = '暂无';
                    }
                    //处理好评率
                    $res[$k]['appraise'] = model('AppraiseModel')->getAppRate(2,$v['tour_id']);

                }
            }

            return $res;
        }

        if($type==2){
            $res = Db::name('collect')->alias('c')->join('hotel h','c.other_id=h.hotel_id','LEFT')
                ->field('hotel_id,hotel_name,hotel_covers,hotel_tag,longitude,latitude,sum_sale')
                ->where('c.uid',$uid)
                ->where('c.type',2)
                ->limit(($page-1)*config('page_size'),config('page_size'))
                ->order('c.create_time','desc')
                ->select();
            if(!empty($res)) {
                foreach ($res as $k => $v) {

                    //查询门票最低价
                    $res[$k]['price'] = Db::name('hotel_room')
                        ->field('room_price')
                        ->where('hotel_id', $v['hotel_id'])
                        ->where('is_display', 0)
                        ->where('is_del', 0)
                        ->min('room_price');
                    //处理景点没上架门票的情况
                    if ($res[$k]['price'] == 0) {
                        $res[$k]['price'] = '暂无';
                    }

                    //处理好评率
                    $res[$k]['appraise'] = model('AppraiseModel')->getAppRate(1, $v['hotel_id']);
                    $res[$k]['range'] = getDistance($latitude, $longitude, $v['latitude'], $v['longitude']);
                }
            }
            return $res;
        }

        if($type==3){
            $res = Db::name('collect')->alias('c')->join('group_tour g','c.other_id=g.id','LEFT')
                ->field('id,gtour_covers,gtour_days,gtour_title,gtour_label,gtour_price,app_sum,sale_sum')
                ->where('c.uid',$uid)
                ->where('c.type',3)
                ->limit(($page-1)*config('page_size'),config('page_size'))
                ->order('c.create_time','desc')
                ->select();
            return $res;
        }

        if($type==4){
            $res = Db::name('collect')->alias('c')->join('food_store f','c.other_id=f.food_store_id','LEFT')
                ->field('food_store_id,food_store_name,food_people_money,food_tag,food_store_longitude,food_store_latitude')
                ->where('c.uid',$uid)
                ->where('c.type',4)
                ->limit(($page-1)*config('page_size'),config('page_size'))
                ->order('c.create_time','desc')
                ->select();
            if(!empty($res)){
                foreach($res as $k =>$v)
                {
                    $res[$k]['range'] = getDistance($latitude,$longitude,$v['food_store_latitude'],$v['food_store_longitude']);
                }
            }

            return $res;
        }

        if($type==5){
            $res = Db::name('collect')->alias('c')->join('method m','c.other_id=m.mid',"left")
                ->join('user u','m.uid=u.uid','left')
                ->field('m.title,m.mid,m.image,u.user_icon,
                u.nickname,m.update_time,m.set_like_num,m.like_num,m.set_share_num,m.share_num')
                ->where('c.uid',$uid)
                ->where('c.type',5)
                ->limit(($page-1)*config('page_size'),config('page_size'))
                ->order('c.create_time','desc')
                ->select();
            if(!empty($res)){
                foreach($res as $k=>$v){
                    //获取
                    $res[$k]['image'] = explode(',',$v['image']);
                    $res[$k]['like_num'] = (int)$v['set_like_num']+$v['like_num'];
                    $res[$k]['share_num'] = (int)$v['set_share_num']+$v['share_num'];
                    $res[$k]['comment'] = Db::name('comment_method')->where('wid',$v['mid'])
                        ->where('pid',0)->where('is_del',0)->where('is_display',0)
                        ->count();
                    $res[$k]['update_time'] = format_date(strtotime($v['update_time']));
                    unset($res[$k]['set_like_num']);
                    unset($res[$k]['set_share_num']);
                }
            }
            return $res;
        }

        if($type==6){
            $res = Db::name('collect')->alias('c')->join('friends_circle m','c.other_id=m.f_id','left')
                ->join('user u','m.uid = u.uid','left')
                ->field('m.fc_title,m.f_id,m.image,m.video,u.user_icon,
                u.nickname,m.update_time,m.set_like_num,m.like_num,m.set_share_num,m.share_num,m.status')
                ->where('c.uid',$uid)
                ->where('c.type',6)
                ->limit(($page-1)*config('page_size'),config('page_size'))
                ->order('c.create_time','desc')
                ->select();
            if(!empty($res))
            {
                foreach ($res as $k => $v) {
                    $res[$k]['comment'] = Db::name('comment_circle')->where('wid',$v['f_id'])
                        ->where('pid',0)->where('is_del',0)->where('is_display',0)
                        ->count();
                    $res[$k]['like_num'] = (int)$v['set_like_num']+$v['like_num'];
                    $res[$k]['share_num'] = (int)$v['set_share_num']+$v['share_num'];
                    $res[$k]['image'] = explode(',',$v['image']);
                    $res[$k]['update_time'] = format_date(strtotime($v['update_time']));
                    unset($res[$k]['set_like_num']);
                    unset($res[$k]['set_share_num']);
                }
            }
            return $res;
        }

    }

   /**
    * [addCollection 添加收藏]
    * @Author   kendy
    * @DateTime 2019-11-14T14:09:25+0800
    */
    public function addCollection()
    {

          $userId = input('post.user_id/d');
          if(empty($userId))
             return apiReturn('400','用户信息为空',0);
          $otherId = input('post.other_id/d');
          if(empty($otherId))
             return apiReturn('400','产品id为空',0);
         $type = input('post.type/d');
          if(empty($type))
             return apiReturn('400','产品类型为空',0);
        $data['uid'] = $userId;
        $data['other_id'] = $otherId;
        $data['type'] = $type;
        $is_collect = Db::name('collect')->where($data)->find();
        if($is_collect)
          return apiReturn('200','收藏成功',0); 
        $data['create_time'] = date('Y-m-d H:i:s',time()); 
        if(in_array($type, [1,2,3,4,5,6]))
        {  
          //攻略
          if($type==5)
          {
            $collect = Db::name('method')
            ->where(['mid'=>$data['other_id'],'is_pass'=>1,'is_del'=>0,'is_display'=>0])
            ->find();
            if(empty($collect))
            return apiReturn('400','无效的攻略信息',0);
            Db::name('method')->where('mid',$otherId)->setInc('collect_num',1); 
          }
          //友圈
          if($type==6)
          {
             $collect = Db::name('friends_circle')
             ->where(['f_id'=>$otherId,'is_pass'=>1,'is_del'=>0,'is_display'=>0])
             ->find();
             if(empty($collect))
             return apiReturn('400','无效的友圈信息',0);
             Db::name('friends_circle')->where('f_id',$otherId)->setInc('collect_num',1);
          }
          //判断是否有该景点信息
          if($type==1)
          {
             $collect = Db::name('tours')
             ->where(['tour_id'=>$otherId,'is_del'=>0,'is_display'=>0,'is_pass'=>1])
             ->find();
             if(empty($collect))
             return apiReturn('400','无效的景点信息',0);

          }
          //判断是否有该酒店信息
          if($type==2)
          {
            $collect = Db::name('hotel')
            ->where(['hotel_id'=>$otherId,'is_del'=>0,'is_pass'=>1])
            ->find();
            if(empty($collect))
            return apiReturn('400','无效的酒店信息',0);
          }
          //判断是否有该跟团产品
          if($type==3)
          {
            $collect = Db::name('group_tour')
            ->where(['id'=>$otherId,'is_del'=>0,'is_display'=>0])
            ->find();
            if(empty($collect))
            return apiReturn('400','无效的跟团信息',0);
          }
           //判断是否有该美食店铺
          if($type==4)
          {
            $collect = Db::name('food_store')
            ->where(['food_store_id'=>$otherId,'is_del'=>0,'is_pass'=>1])
            ->find();
            if(empty($collect))
            return apiReturn('400','无效的美食店铺信息',0);
          }
          //添加到收藏表
            $rs = Db::name('collect')
            ->insert($data);
            if(false !==$rs){
                return apiReturn('200','收藏成功',0);    
            }
          return apiReturn('200','收藏失败',0);    
   }
}




    
   /**
    * [getProCollectionnNum 查看产品的收藏数量]
    * @Author   kendy
    * @DateTime 2019-11-15T09:48:05+0800
    * @param    [type]                   $otherId [景点/酒店/跟团/美食/攻略/导游 ID]
    * @param    [type]                   $type    [1景点，2酒店，3跟团，4美食，5攻略，6导游]
    * @return   [type]                            [description]
    */
   public function getProCollectionnNum($otherId,$type)
   {
         return Db::name('collect')
         ->where(['other_id'=>$otherId,'type'=>$type])
         ->count(); 

   }
    
    
    /**
     * [cancelCollection 取消收藏]
     * @Author   kendy
     * @DateTime 2019-12-11T15:53:54+0800
     * @return   [type]                   [description]
     */
    public function cancelCollection()
    {
         $userId = input('post.user_id/d');
          if(empty($userId))
             return apiReturn('400','用户信息为空',0);
          $otherId = input('post.other_id/d');
          if(empty($otherId))
             return apiReturn('400','产品id为空',0);
         $type = input('post.type/d');
          if(empty($type))
             return apiReturn('400','产品类型为空',0);    
          $data['uid'] = $userId;
          $data['other_id'] = $otherId;
          $data['type'] = $type;
        $info = Db::name('collect')
        ->where($data)
        ->delete();
        if(false !== $info)
        {
            return apiReturn('200','取消收藏成功',0);   
        }
        return apiReturn('400','取消收藏失败',0);   
    }
}